package elitech.vietnam.weatherfashion.custom;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import elitech.vietnam.weatherfashion.R;
import elitech.vietnam.weatherfashion.config.Const;
import elitech.vietnam.weatherfashion.config.Utils;
import elitech.vietnam.weatherfashion.custom.dialogues.DialogUserInfo;
import elitech.vietnam.weatherfashion.dto.CommentDTO;

public class CommentAdapter extends ArrayAdapter<CommentDTO> {

	private Context context;
	private List<CommentDTO> data;
	
	public CommentAdapter(Context context, int textViewResourceId,
			List<CommentDTO> objects) {
		super(context, textViewResourceId, objects);
		// TODO Auto-generated constructor stub
		this.data = objects;
		this.context = context;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View view = convertView;
		final CommentHolder holder;
		if (view == null) {
			view = LayoutInflater.from(context).inflate(R.layout.layout_comment, null);
			holder = new CommentHolder();
			holder.data = data.get(position);
			holder.image = (ImageView) view.findViewById(R.id.comment_userimage);
			holder.name = (TextView) view.findViewById(R.id.comment_username);
			holder.comment = (TextView) view.findViewById(R.id.comment_content);
			holder.more = (TextView) view.findViewById(R.id.comment_viewmore);
			Utils.setTextUnderline(holder.more);
			
		} else {
			holder = (CommentHolder) view.getTag();
		}
		
		holder.name.setText(holder.data.getMemberName());
		//ImageLoader.getInstance().displayImage(AppConfig.SERVER_IMAGE_URL + holder.data.getMemberImage(), holder.image);
		UrlImageViewHelper.setUrlDrawable(holder.image, Const.SERVER_IMAGE_URL + holder.data.getMemberImage(), R.drawable.no_avatar);
		if (holder.data.getCommentContent().length() > Const.COMMENT_CHAR_LIMIT) {
			holder.comment.setText(Utils.trimString(holder.data.getCommentContent(), Const.COMMENT_CHAR_LIMIT));
			holder.more.setVisibility(View.VISIBLE);
		} else {
			holder.comment.setText(holder.data.getCommentContent());
			holder.more.setVisibility(View.GONE);
		}
		
		holder.image.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				new DialogUserInfo(getContext(), data.get(position).getMemberId()).show();
			}
		});
		holder.name.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
		});
		holder.more.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				holder.comment.setText(holder.data.getCommentContent());
				holder.more.setVisibility(View.GONE);
			}
		});
		
		return view;
	}
	
	public void addItem(CommentDTO data, LinearLayout layout, TextView text_commentcount, TextView text_p_commentcount) {
		if (data == null)
			return;
		this.data.add(data);
		layout.addView(getView(this.data.size() - 1, null, null));
		text_commentcount.setText(this.data.size() + context.getString(R.string._comments));
		if (text_p_commentcount!=null) 
			text_p_commentcount.setText(String.valueOf(this.data.size()));
	}
	
	public void LoadList(LinearLayout layout) {
		if (data == null || data.isEmpty())
			return;
		for (int i = 0; i<data.size(); i++) 
			layout.addView(getView(i, null, null));
	}
	
	public List<CommentDTO> getComments() {
		return this.data;
	}
	
	static class CommentHolder {
		CommentDTO data;
		ImageView image;
		TextView name;
		TextView comment;
		TextView more;
	}
}
